import docker
import os
import time

# Create container and start it
client = docker.APIClient(base_url='unix://var/run/docker.sock')
container = client.create_container('clockworksoul/zork2:latest', name='test', stdin_open = True, tty = True, command = 'sh')
client.start(container)

# Create communication socket
s = client.attach_socket(container, {'stdin': 1, 'stdout': 1, 'stream':1})

# Set the socket as non-blocking
s._sock.setblocking(False)

# Start communication by sending cat
os.write(s.fileno(),b'take sword\n')

# Since we are non-blocking, wait for a while to get the output
time.sleep(1)

# Read up-to 10000 bytes. If there are more, we can issue another read
print(os.read(s.fileno(),10000))

client.stop(container)
client.wait(container)
client.remove_container(container)